package com.neusoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.neusoft.base.BaseServiceImpl;
import com.neusoft.config.Pagination;
import com.neusoft.mapper.UserMapper;
import com.neusoft.model.UserSearch;
import com.neusoft.po.User;
import com.neusoft.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 访问用户服务实现
 *
 * @author Lucifer
 * @since 2021/5/16 8:10
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {


    @Resource
    private UserMapper userMapper;

    @Override
    public BaseMapper<User> getBaseDao() {
        return userMapper;
    }

    @Override
    public Integer save(User entity) {
        Integer id = entity.getId();
        if (null == id) {
            entity.setCreate_time(new Date());
            insert(entity);
            id = entity.getId();
        } else {
            entity.setUpdate_time(new Date());
            updateById(entity);
        }
        return id;
    }

    @Override
    public User login(String username, String password) {
        User user = userMapper.selectOne(new QueryWrapper<User>()
                .eq("username", username).eq("password", password));
        if (null == user) {
            throw new RuntimeException("用户名或者密码错误");
        }
        return user;
    }

    @Override
    @Pagination
    public Object page(UserSearch userSearch) {
        return userMapper.list(userSearch);
    }
}
